package com.sl.biz.modular.process.controller;

import com.google.common.collect.Maps;
import com.sl.plugin.step.json.BizFuncListEnum;
import com.sl.biz.modular.process.controller.request.SquareLakeProcessQueryByConditionRequest;
import com.sl.biz.modular.process.controller.request.SquareLakeProcessRemoveByIdRequest;
import com.sl.biz.modular.process.controller.response.ProcessSimpleVO;
import com.sl.biz.modular.process.controller.response.ProcessVO;
import com.sl.biz.modular.process.dao.domain.entity.ProcessConfigDO;
import com.sl.biz.modular.process.service.ProcessConfigBizService;
import com.sl.core.engine.meta.valuemeta.FieldTypeValueMetaCollect;
import com.sl.core.engine.rowset.ProcessRowSetEnum;
import lombok.RequiredArgsConstructor;
import org.apache.commons.compress.utils.Lists;
import org.apache.hop.core.logging.LogLevel;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/process")
@RequiredArgsConstructor
public class ProcessConfigController {
    private final ProcessConfigBizService processBizService;

    @PostMapping("/save")
    public String save(@RequestBody @Validated ProcessConfigDO addRequest) {
        return processBizService.save(addRequest);
    }

    @PostMapping("/log_level_list")
    public List<Map<String, String>> logLevelList() {
        List<Map<String, String>> resMapList = Lists.newArrayList();
        for (LogLevel value : LogLevel.values()) {
            Map<String, String> resMap = Maps.newHashMap();
            resMap.put("code", value.getCode());
            resMap.put("name", value.getDescription());
            resMapList.add(resMap);
        }
        return resMapList;
    }

    @PostMapping("/field_type_list")
    public List<Map<String, Object>> fieldTypeList() {
        return FieldTypeValueMetaCollect.getFieldType();
    }

    @PostMapping("/func_list")
    public List<Map<String, Object>> funcList() {
        return Arrays.stream(BizFuncListEnum.values()).map(v -> {
            Map<String, Object> map = Maps.newHashMap();
            map.put("code", v.getCode());
            map.put("name", v.getName());
            return map;
        }).toList();
    }

    @PostMapping("/row_set_list")
    public List<Map<String, Object>> getRowSetList() {
        return Arrays.stream(ProcessRowSetEnum.values()).map(v -> {
            Map<String, Object> map = Maps.newHashMap();
            map.put("code", v.getCode());
            map.put("name", v.getName());
            return map;
        }).toList();
    }


    @PostMapping("/get_by_id")
    public ProcessVO getById(@RequestBody @Validated ProcessConfigDO addRequest) {
        return processBizService.getById(addRequest);
    }

    @PostMapping("/list")
    public List<ProcessSimpleVO> list(@RequestBody @Validated SquareLakeProcessQueryByConditionRequest queryByConditionRequest) {
        return processBizService.list(queryByConditionRequest);
    }

    @PostMapping("/remove_by_id")
    public Boolean removeById(@RequestBody @Validated SquareLakeProcessRemoveByIdRequest removeByIdRequest) {
        return processBizService.removeById(removeByIdRequest);
    }


}
